home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
(A)TB
/
(A)TBR.ADF
/
CLI_Only
/
TinyType130.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-22
|
18KB
|
470 lines
/*****************************************************************************\
* *
* TinyType.c V1.30 *
* *
* By Christopher Weber (c) 1991 - Freely distributable as long as the *
* source code and documentation files *
* are kept unaltered and together. *
* *
* Based on PinHead2 By Frank Ch. Eigler (c) 1988 *
* *
* *
* *
* This is Not Shareware - it is free. *
* *
* This is version 1.20 of TinyType. This program is a modification of the *
* source for PinHead2 a wonderfully useful program written by Frank Ch. *
* Eigler. This program has one main advantage over PinHead2 - it allows *
* the user to print either all the even numbered pages or all of the odd *
* numbered pages at one time. *
* *
\*****************************************************************************/
#include <stdio.h>
#include <ctype.h>
#define LINES 144 /* No. of lines of text we'll print */
#define COLUMNS 160 /* No. of columns we'll print */
/* Prototype functions used */
void PrintPage(void); /* Print a page */
void ReadPage(void); /* Read in a page - based on format */
void HelpTxt(void); /* Print Help text for user */
char PText[LINES][COLUMNS + 1]; /* Actual buffer where we put the */
/* text before we print it */
/* Note - we won't use a 00 byte to */
/* terminate the strings. */
short Pages = 1; /* Page counter.. */
short NumColumns; /* No. of columns to be formatted */
short ColWidth; /* No. of chars per column */
short TabSize; /* No. of spaces / tab */
short PrintFooter; /* Whether or not to print footer */
short StartPage; /* First page to print */
short EndPage; /* Last page to print */
short Query; /* Query mode flag */
short Odd; /* Odd page flag */
short Even; /* Even page flag */
short Count; /* Count pages flag */
FILE *InFile = NULL; /* Disk file where we get text from */
FILE *Printer = NULL; /* Printer - AmigaDOS PAR: */
char FName[80]; /* File name */
char Scrap[10]; /* Used to get user input */
char ColSpaces[7]; /* No. of spaces between columns */
/* The init codes for Epson printers */
char Page_Init[] = "\x1B3\x0F\x1BQ\xFF\x1BM\x0F\x1BS0\x1BU1";
/*
** Here's what the codes mean so you can change them for some non-Epson printer
** Note: $1B is written as the mnemonic <esc>
**
** <esc>3<$0F> - sets spacing to 15 ($F) 216th's of an inch
** <esc>Q<$FF> - clears right margin - sets to "infinite" 255
** <esc>M - sets "elite" pitch (96 chars / line on my printer)
** <$0F> - sets condensed print mode (cond. elite: 160 chars/line)
** <esc>S0 - sets superscript char set
** <esc>U1 - sets unidirectional mode - for perfect alignment
** (bidirectional wouldn't speed things up anyway)
*/
char *HelpText[] =
{
"\nUse: TType [filename] [-C<columns>] [-F] [-T<tab>] [-S<page>]",
" [-L<page>] [-O] [-E] [-P] [-Q]",
"Options:",
" -C<columns> : sets no. of columns printed - 1 to 8 (default 2)",
" -F : supresses printing of file name and page #",
" -T<tab> : sets tab size - 3 to 16 (default 8)",
" -S<page> : sets first page to print (default 1)",
" -L<page> : sets last page to print (default EOF)",
" -O : print odd numbered pages",
" -E : print even numbered pages",
" -P : give total no. of TinyType pages in document. ",
" Doesn't print anything. Uses -C and -T options to",
" determine page format.",
" -Q : sets query mode - asks after each page whether or",
" not to continue. This overrides the Ending Page.",
"",
"Only EPSON-compatible printers are supported. This program bypasses the",
"AmigaDOS PRT: device and writes to the parallel port directly. All",
"unprintable (ASCII codes 128 - 255) as well as unused control codes are",
"filtered out. The only control codes used are CR and LF, and TAB..",
NULL
};
void main(argc,argv)
int argc; /* Not short.. Might be 32-bit int */
char *argv[];
{
short i,Tmp;
printf("TinyType V1.30\n");
printf("By Christopher Weber (c)1991 - see docs for distribution policy\n");
srand((short)time(NULL));
if((* argv[1] == '?') || (argc == 1)) /* Print help text */
HelpTxt();
PrintFooter = 1; /* Set up defaults */
NumColumns = 2;
TabSize = 8;
StartPage = 1;
EndPage = -1;
Query = 0;
Odd = 0;
Even = 0;
Count = 0;
for(i=1;i<argc;i++) /* Look through arguments */
{
if(argv[i][0] == '-') /* Control codes maybe.. */
switch(toupper(argv[i][1])) /* Let's look at code */
{
case 'C':
Tmp = atoi(& argv[i][2]);
if(Tmp >= 1 && Tmp <= 8)
NumColumns = Tmp;
break;
case 'F':
PrintFooter = 0;
break;
case 'T':
Tmp = atoi(& argv[i][2]);
if(Tmp >= 3 && Tmp <= 16)
TabSize = Tmp;
break;
case 'S':
StartPage = atoi(& argv[i][2]);
break;
case 'L':
EndPage = atoi(& argv[i][2]);
break;
case 'O':
Odd = 1;
break;
case 'E':
Even = 1;
break;
case 'Q':
Query = 1;
break;
case 'P':
Count = 1;
break;
default: /* Invalid option - inform user */
{
printf("\nInvalid option : -%c\n",toupper(argv[i][1]));
HelpTxt();
}
}
else if(InFile == NULL) /* If no file yet, try to open this */
{
InFile = fopen(argv[i],"r");
strcpy(FName,argv[i]);
}
}
if(InFile==NULL) /* User gave us an invalid file or no file.. */
{
if(FName == NULL)
printf("\nNo File name given.\n");
else
printf("\nFile \"%s\" not found.\n",FName);
HelpTxt();
}
/* Figure out page layout before we do anything. */
if(NumColumns >= 2) /* If several columns */
{
ColWidth = (COLUMNS / NumColumns) - 1;
/*
** Here, we have a problem. If the user asked for some weird # of columns
** like 7, the columns will have strange widths, and an odd number of spaces.
** This little routine accounts for all the extra spaces, and then distributes
** them among the columns, as evenly as possible. That's all there is to this
** ugly contraption..
*/
Tmp = COLUMNS - (ColWidth * NumColumns);
for(i=0;i<NumColumns-1;i++) /* Distribute integer spaces */
ColSpaces[i] = Tmp / (NumColumns - 1);
/* Subtract # already taken up. */
Tmp -= (NumColumns - 1) * ColSpaces[0];
for(i=0;i<Tmp;i++) /* Randomly distribute the rest.. */
ColSpaces[rand() % (NumColumns-1)] ++;
}
else
{
ColWidth = COLUMNS; /* 1 column - one BIG column.. */
for(i=0;i<8;i++) /* No spaces at all.. */
ColSpaces[i] = 0;
}
/* IT WORX!!! */
/* Ok. We know the page layout - now to actually do something. */
if(Count) /* Count total number of pages in doc */
{ /* but don't print anything. */
do /* This will ignore Start and End */
{ /* values. */
ReadPage();
Pages++;
} while(! feof(InFile));
printf("\nTotal # of TType pages in doc. are : %d\n", Pages - 1);
fclose(InFile);
exit(0);
}
/* Is the Ending page number legal? */
if((EndPage != -1) && (EndPage < StartPage))
{
printf("\nWarning. The Ending Page no. is illegal - has been set to\n");
printf("EOF. Proceed? (Y/N) :");
gets(Scrap);
if(toupper(Scrap[0]) == 'Y')
EndPage = -1;
else
{
fclose(InFile);
printf("Exiting.\n");
exit(0);
}
}
if(Even) /* If we're printing even numbered pages */
{
Pages = 2; /* set page count to reflect even pages */
ReadPage(); /* skip first page */
if (StartPage == 1)
StartPage = 2; /* Then the default first page is changed */
if (EndPage == -1)
EndPage = -2; /* and the end page is changed */
}
/* Are any Start and End values given correct for print mode we are using? */
if( (Odd && ((EndPage%2 == 0) || (StartPage%2==0))) ||
(Even && ((EndPage%2 != 0) || (StartPage%2 != 0))) )
{
printf("\nWarning. The Ending or Starting Page no. is illegal.\n");
printf("Starting and Ending page numbers must be ");
if(Odd)
printf("odd.\n");
else
printf("even.\n");
fclose(InFile);
exit(0);
}
Printer = fopen("PAR:","w"); /* Try to open printer */
if(Printer == NULL) /* Not available... */
{
fclose(InFile);
printf("\nParallel printer not available.\n\n");
Delay(40L);
exit(0);
}
fprintf(Printer,"\x1B@"); /* Reset printer.. */
printf("\nWorking.\n");
printf("\nPrinting %d chars on %d columns.\n",ColWidth,NumColumns);
if(TabSize > ColWidth)
{
printf("\nWarning. Tab size is greater than column width. It has\n");
printf("been reduced to 8. Proceed? (Y/N) :");
gets(Scrap);
if(toupper(Scrap[0]) == 'Y')
TabSize = 8;
else
{
fclose(InFile);
fclose(Printer);
exit(0);
}
}
do /* Print a page each time through */
{
printf("Reading Page %d : ",Pages);
fflush(stdout); /* Force printing of message above */
ReadPage(); /* Load a page */
if(Pages >= StartPage) /* Print this page? */
{
printf("Printing.\n");
PrintPage(); /* Yes.. */
}
else
printf("Bypassing.\n"); /* Ignore this page.. */
if(Query) /* Well, query... */
{
printf("Continue on to page ");
if (Odd || Even)
printf("%d? (Y/N) :",Pages+2);
else
printf("%d? (Y/N) :",Pages+1);
gets(Scrap);
if(toupper(Scrap[0]) == 'N')
break;
}
else if(Pages == EndPage) /* User don't wanna see no more.. */
{
printf("Preset last page done.\n");
break;
}
if(Odd || Even) /* If we are only printing Odd or Even */
{ /* pages then we need to skip a page. */
Pages = Pages + 2;
ReadPage(); /* Load a page and skip it */
}
else
Pages++; /* Increment page counter */
} while(! feof(InFile)); /* Go on, ad inifinitum.. */
fprintf(Printer,"\x1B@"); /* Reset printer */
printf("\nFinished.\n");
fclose(Printer); /* Close open files */
fclose(InFile);
}
void PrintPage()
{
short i,x;
fprintf(Printer,"%s",Page_Init); /* Init printer for page */
for(i=0;i<LINES;i++) /* Print all lines */
fprintf(Printer,"%s", PText[i]);
fprintf(Printer,"\n"); /* Leave a blank line */
if(PrintFooter) /* Conditionally print footer */
fprintf(Printer,"[TType] \x0EFile \"%s\" Page %d",FName,Pages);
fprintf(Printer,"\x0C"); /* Go to next page - form feed */
}
void ReadPage()
{
short x,y,col; /* Co-ordinates of "cursor" */
unsigned char c;
short XStart, XEnd; /* Column co-ordinates */
for(y=0;y<LINES;y++) /* Clear page totally */
{
PText[y][COLUMNS + 1] = ''; /* NULL terminate each line */
for(x=0;x<COLUMNS;x++)
{
PText[y][x] = ' ';
}
}
x = y = col = 0;
XStart = 0;
XEnd = ColWidth-1;
while(1)
{
c = getc(InFile); /* Read a character from the file */
if(feof(InFile)) /* Ugly check for EOF.. Die Fast! */
return;
switch(c) /* Let us parse.. */
{
case '\n':
case '\r':
x = XStart; /* Go to next line / column / page */
y ++;
break;
case '\t':
do
{
x++;
} while((x % TabSize) && (x < XEnd));
break;
default:
if(isprint(c)) /* REAL text!... Aaahhhh */
{
PText[y][x] = c; /* Put it in array, advance cursor */
x ++;
if(x > XEnd) /* Remove excess CR / LFs */
{
c = getc(InFile);
if(c!='\n' && c!='\r')
ungetc(c,InFile);
}
}
}
if(x > XEnd) /* If at end of column width */
{
y++; /* Next line! */
x = XStart;
}
if(y == LINES)
{
col ++; /* Next column! - adjust XStart/End */
if(col == NumColumns)
return; /* Finished all columns. Return. */
y = 0; /* Top of column */
/* Calculate new XStart */
XStart += ColWidth + ColSpaces[col-1];
XEnd = XStart + ColWidth-1; /* Calculate new XEnd */
x = XStart;
}
}
}
void HelpTxt()
{
short i;
for(i=0;HelpText[i];i++)
printf("%s\n",HelpText[i]);
exit(0);
}